package com.frank.towerrush.activity;

import com.frank.towerrush.R;
import com.frank.towerrush.service.SoundService;
import com.frank.towerrush.util.SystemMethod;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class StartActivity extends BaseActivity implements OnClickListener, DialogInterface.OnClickListener {

    TextView txtTouch;
    TextView txtStart;
    TextView txtSetting;
    TextView txtHelp;

    Button btnExit;

    @Override
    protected void init() {
        setContentView(R.layout.activity_start);
        // 初始化系统
        SystemMethod.systemInit(this);

        // 获取控件
        txtTouch = (TextView) findViewById(R.id.start_txt_touch);
        txtStart = (TextView) findViewById(R.id.start_txt_start);
        txtSetting = (TextView) findViewById(R.id.start_txt_setting);
        txtHelp = (TextView) findViewById(R.id.start_txt_help);
        btnExit = (Button) findViewById(R.id.start_btn_exit);
    }

    @Override
    protected void event() {
        // 控件监听
        findViewById(R.id.start_main).setOnClickListener(this);
        txtStart.setOnClickListener(this);
        txtSetting.setOnClickListener(this);
        txtHelp.setOnClickListener(this);
        btnExit.setOnClickListener(this);
    }

    @Override
    protected void loadData() {
        // Do nothing
    }

    private void exit() {
        // 弹出dialog确认退出
        new AlertDialog.Builder(this).setTitle(R.string.start_exit).setPositiveButton(R.string.cancel, this)
                .setNegativeButton(R.string.ok, this).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 退出游戏
            exit();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.start_main:
            showMenu();
            break;
        case R.id.start_txt_start:
            // 开始游戏
            startActivity(SelectActivity.class, false);
            break;
        case R.id.start_txt_setting:
            // 游戏设置
            startActivity(SettingActivity.class, false);
            break;
        case R.id.start_txt_help:
            // 游戏帮助
            startActivity(HelpActivity.class, false);
            break;
        case R.id.start_btn_exit:
            // 退出游戏
            exit();
            break;
        default:
            break;
        }
    }

    private void showMenu() {
        // 显示菜单
        txtTouch.setVisibility(View.GONE);
        txtStart.setVisibility(View.VISIBLE);
        txtSetting.setVisibility(View.VISIBLE);
        txtHelp.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_NEGATIVE) {
            // 把activity队列中所有activity关闭
            for (int i = 0; i < activityQueue.size(); i++) {
                activityQueue.get(activityQueue.keyAt(i)).finish();
            }

            // 关闭音乐
            SoundService.getService().musicStop();

            // 保存偏好

            SystemMethod.savePreferences(this);
        }
    }

}
